﻿package {
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.text.TextField;
	import flash.display.Sprite;
	
	public class GravityTest extends MovieClip {
		
		private var grav:Number = 0;
		private var gacc:Number = 0.1;
		private var jumpspeed:Number = -2;
		private var falling:Boolean = false;
		private var running:Boolean = false;
		private var jumped:Boolean = false;
		
		private var level:Sprite;
		private var ground:Ground;
		private var man:Man;
		private var wall:Wall;
		private var status:TextField;

		
		//private var bx:Number;
//		private var by:Number;
		private var bxspeed = 5;
		
		
		//keys down checks
		private var up:Boolean = false;
		private var down:Boolean = false;
		private var right:Boolean = false;
		private var left:Boolean = false;
		
		public function GravityTest() {
			init();
		}
		
		private function init() {
			
			level = new Sprite();
			addChild(level);
			
			man = new Man();
			addChild(man);
			man.gotoAndStop(1);
			
			ground = new Ground();
			level.addChild(ground);
			ground.x = stage.stageWidth/2;
			ground.y = stage.stageHeight-ground.height/2;
			
			man.x = stage.stageWidth/2;
			man.y = ground.y //- man.height/2;
			
			wall = new Wall();
			level.addChild(wall);
			wall.x = stage.stageWidth/2 + stage.stageWidth/4;
			wall.y = ground.y;
			
			status = new TextField();
			status.text = "Game Over";
			status.scaleX = status.scaleY = 2;
			status.x = stage.stageWidth/2;
			status.y = stage.stageHeight/2;
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, myKeyUp);
		}
		
		public function onEnterFrame(event:Event) {
			if (man.hitTestObject(ground)) {
				falling = false;
				//man.y = ground.y - ground.height/2 - man.height/2;
				grav = 0;
				if (!running) {
					man.gotoAndStop(1);
				}
			}
			
			if (jumped && running && !falling) {
				man.gotoAndStop(2);
				jumped = false;
			}
			
			if (!falling && up) {
					grav = jumpspeed;
					man.y += grav;
					falling = true;
					man.gotoAndStop(3);
					jumped = true;
			}
			
			if (falling) {
				man.y += grav;
				grav += gacc;
			}
			
			if (left) {
				level.x += bxspeed;
			}
			
			if (right) {
				level.x -= bxspeed;
			}
			
			if (man.hitTestObject(wall)) {
				addChild(status);
			}
		}
		
		private function myKeyDown(event:KeyboardEvent):void {
			
			if (event.keyCode == Keyboard.UP && !down) {
				up = true;
			}
			
			if (event.keyCode == Keyboard.RIGHT && !left) {
				right = true;
				if (!running && !falling) {
					man.gotoAndStop(2);
					running = true;
				}
			}
			
			if (event.keyCode == Keyboard.LEFT && !right) {
				left = true;
				if (!running && !falling) {
					man.gotoAndStop(2);
					running = true;
				}
			}
			
			if (event.keyCode == Keyboard.DOWN && !up) {
				down = true;
			}
			
			
		}
		
		private function myKeyUp(event:KeyboardEvent):void {
			
			if (event.keyCode == Keyboard.UP) {
				up = false;
			}
			
			if (event.keyCode == Keyboard.RIGHT) {
				right = false;
				if (running && !left) {
					man.gotoAndStop(1);
					running = false;
				}
			}
			
			if (event.keyCode == Keyboard.LEFT) {
				left = false;
				if (running && !right) {
					man.gotoAndStop(1);
					running = false;
				}
			}
			
			if (event.keyCode == Keyboard.DOWN) {
				down = false;
			}
		}
	}
}